package com.orhon.smartcampus.modules.teacher.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.material.entity.Buildings;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.entity.Unit;
import com.orhon.smartcampus.modules.material.gqlModel.GQLBuilding;
import com.orhon.smartcampus.modules.material.gqlModel.GQLFloor;
import com.orhon.smartcampus.modules.material.gqlModel.GQLRoom;
import com.orhon.smartcampus.modules.material.gqlModel.GQLUnit;
import com.orhon.smartcampus.modules.material.mm.BuildingMapper;
import com.orhon.smartcampus.modules.material.mm.FloorMapper;
import com.orhon.smartcampus.modules.material.mm.RoomMapper;
import com.orhon.smartcampus.modules.material.mm.UnitMapper;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.teacher.dao.AwardDao;
import com.orhon.smartcampus.modules.teacher.dao.OfficeArrangeDao;
import com.orhon.smartcampus.modules.teacher.entity.Award;
import com.orhon.smartcampus.modules.teacher.entity.OfficeArrange;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLAward;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacherOffice;
import com.orhon.smartcampus.modules.teacher.mm.AwardMapper;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.teacher.mm.TeacherOfficeMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TeacherOfficeFetcher implements DataFetcher  {
	
	@Autowired
    OfficeArrangeDao officeArrangeDao;

    @Autowired
    TokenService tokenService;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);
        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from OfficeArrange where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        OfficeArrange item =  officeArrangeDao.findId(hql, null);
        if(item!=null) {

            GQLBuilding gqlbuildings = null;
            if (env.getSelectionSet().contains("buildings")){
                Buildings buildings = item.getMaterialBuildingsByBuildingsId();
                gqlbuildings = BuildingMapper.MAPPER.toBuilding(buildings);
            }

            GQLUnit gqlUnit = null;
            if(env.getSelectionSet().contains("unit")){
                Unit unit = item.getMaterialUnitByUnitId();
                gqlUnit = UnitMapper.MAPPER.toUnit(unit);
            }

            GQLFloor gqlFloor = null;
            if(env.getSelectionSet().contains("floor")){

                Floor floor = item.getMaterialFloorByFloorId();
                gqlFloor = FloorMapper.MAPPER.toFloor(floor);
            }

            GQLRoom gqlRoom = null;
            if(env.getSelectionSet().contains("room")){
                Room room = item.getMaterialRoomByRoomId();
                gqlRoom = RoomMapper.MAPPER.toRoom(room);
            }

            GQLDepartment gqlDepartment = null;
            if(env.getSelectionSet().contains("department")){

                OrgDepartments orgDepartments = item.getSystemctlOrgDepartmentsByDepartmentId();
                gqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(orgDepartments);

            }

            GQLTeacher gqlTeacher = null;
            if(env.getSelectionSet().contains("headUser")){

                TInformation teacher = item.getTeacherTInformationByHeadUser();
                gqlTeacher = TeacherMapper.MAPPER.toTeacher(teacher);
            }

            GQLTeacherOffice teacherOffice = TeacherOfficeMapper.MAPPER.mapEntity(item);
            teacherOffice.setBuildings(gqlbuildings);
            teacherOffice.setUnit(gqlUnit);
            teacherOffice.setFloor(gqlFloor);
            teacherOffice.setRoom(gqlRoom);
            teacherOffice.setDepartment(gqlDepartment);
            teacherOffice.setHeadUser(gqlTeacher);

//            GQLTeacherOffice mapEntity = TeacherOfficeMapper.MAPPER.mapEntity(item);
            return teacherOffice;
        }
		return null;
        
    }
}
